package com.moorecat.app;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.app.provider.DBHelper;
import com.moorecat.bean.Shop;
import com.moorecat.http.HttpUtils;
import com.moorecat.utils.ui.ProgressLinearLayout;

/**
 * 最近浏览
 * 
 * @author Minglong.Yu
 * @version 1.0
 * @created Jun 25, 2011 12:49:12 AM
 */
public class RecentlyView extends Activity implements OnItemClickListener,
		OnClickListener {
	private ListView listView;
	private BusinessAdapter businessAdapter;
	private List<Shop> shopList = new ArrayList<Shop>();
	boolean isCoupon;
	private MooerApplication mooerApp;
	private ProgressLinearLayout footer;
	private long taskId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.recently_view_list);
		mooerApp = (MooerApplication) getApplication();
		listView = (ListView) findViewById(R.id.listview);
		footer = (ProgressLinearLayout) LayoutInflater.from(this).inflate(
				R.layout.progress_small, null);
		footer.initView();
		listView.addFooterView(footer);
		businessAdapter = new BusinessAdapter(this, shopList, false);
		listView.setAdapter(businessAdapter);
		listView.setOnItemClickListener(this);
		footer.setOnClickListener(this);
		new BusinessRecentlyViewListTask().execute("");
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		Shop shop = shopList.get(position).copy();
		Intent intent = new Intent(this, Business.class);
		intent.putExtra("businessInfo", shop);
		startActivity(intent);
	}

	public class BusinessRecentlyViewListTask extends
			AsyncTask<String, Integer, List<Shop>> {

		@Override
		protected List<Shop> doInBackground(String... arg0) {
			DBHelper dbHelper = mooerApp.getDbHelper();
			List<Shop> list = dbHelper.selectAll();
			for (Shop shop : list) {
				shop.setIcon(mooerApp.getShopListDefaultIcon());
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<Shop> result) {
			super.onPostExecute(result);
			if (result == null || result.size() == 0) {
				footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
						View.GONE);
				footer.setText(null, "您近期无任何浏览记录");
			} else {
				listView.removeFooterView(footer);
				shopList.addAll(result);
				businessAdapter.notifyDataSetChanged();
				new BitmapTask().execute("");
			}
		}

	}

	private class BitmapTask extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			long id = System.currentTimeMillis();
			taskId = id;
			// 增加一个缓存List避免列表长度变化时遍历发生错误
			List<Shop> cacheList = new ArrayList<Shop>();
			cacheList.addAll(shopList);
			Iterator<Shop> it = cacheList.iterator();
			while (it.hasNext()) {
				/*
				 * 避免重复下载,如果有新的线程,则停止老的线程
				 */
				if (id != taskId) {
					break;
				}
				Shop shop = it.next();
				Bitmap icon = null;
				try {
					if (shop.getIcon() == mooerApp.getShopListDefaultIcon()) {
						icon = HttpUtils.downloadBitmap(shop.getLogo());
						if (icon != null) {
							shop.setIcon(icon);
							publishProgress(0);
						}
					}
				} catch (Exception e) {

				}
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			businessAdapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
}
